package cn.diancan.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import cn.diancan.config.Config;
import cn.diancan.config.Dir;
import cn.diancan.config.ImageSize;
import cn.diancan.util.CutImg;
import cn.diancan.util.FileHandle;
 
public class YmDownLoadAction extends BaseAction{

	
	private Integer width;
	private Integer height;
	private Long imageId;//id
	private Long imageLength;
	private String imageSuffix;// jpg   png
	private String dirCode;
	private String signCode;
	
	public String downImage(){
		//1.找到文件
		try { 
			String fileName =imageId+"-"+imageLength+"_"+imageSuffix;
			if(null==dirCode){
				return null;
			}
			Dir dir = Config.dirs.get(dirCode);
			if(null== dir){
				 return null;
			}
	/*	本地測試帶有端口號，需要注釋	if(!getBasePath2().equals(dir.getAccesshost())){
				return null;
			}*/
			
			String fileAllName= dir.getRootpath()+"/ImageFileA"+FileHandle.parsePath(imageId)+File.separator+fileName;
			File file = new File(fileAllName);
			if (!(file.exists() && file.isFile())){//找不到原图
				return null;
			}
			if(null!= width && null!= height){//需要切图
				for (int i = 0; i <  Config.imageSizes.size(); i++) {
					ImageSize imaget = Config.imageSizes.get(i);
					if(imaget.getWidth().equals(width) && imaget.getHeight().equals(height) ){
						String outImageFileName=file.getPath().replace("_", "_"+width+"X"+height+"_");
						File  newfileImage=new File(outImageFileName);
						if (!(newfileImage.exists() && newfileImage.isFile())){//找不到被切过的图
							if(CutImg.calcAndCutImage(file.getPath(), outImageFileName, width, height)){//切图失败显示原图
								file=new File(outImageFileName);
							}
						}else{
							file=newfileImage;
						}
						break;
					}else if(i== (Config.imageSizes.size()-1)){//找不到
						return null;
					}
				}
			}
			
			if (file.exists() && file.isFile()){
				 FileInputStream fis = new FileInputStream(file);
				 int length = (int)file.length();
        		         byte[] data = new byte[length];
        		         fis.read(data);
        		         fis.close();
        		         if(imageSuffix.equalsIgnoreCase("png")){
        		        	 getResponse().setContentType("image/png");
        		         }else if(imageSuffix.equalsIgnoreCase("jpg")){
        		        	 getResponse().setContentType("image/jpeg");
        		         }else if(imageSuffix.equalsIgnoreCase("gif")){
        		        	 getResponse().setContentType("image/gif");
        		         }else{
        		        	 return null;
        		         } 
        		         getResponse().setContentLength(data.length);
				 getResponse().getOutputStream().write(data);
				 getResponse().getOutputStream().flush();
				 //getResponse().getOutputStream().close();
				 return null;
			} 
		} catch (Exception e) { 
			e.printStackTrace();
		} 
		return null;
	}
	
	//根据url
	public String downUploadImage(){
		try {
			if(null==dirCode){
				return null;
			}
			Dir dir = Config.dirs.get(dirCode);
			if(null== dir){
				 return null;
			}
			if(!getBasePath2().equals(dir.getAccesshost())){
				return null;
			}
			String uri = getRequest().getParameter("uri");
			File newFile = new File(dir.getRootpath()+"/"+"uploadImage/"+uri);
			if (newFile.exists() && newFile.isFile()){
				 FileInputStream fis = new FileInputStream(newFile);
				 int length = (int)newFile.length();
    			     	 byte[] data = new byte[length];
    			     	 fis.read(data);
    			     	 fis.close();
    			     	 getResponse().setContentType("image/jpeg");
    			     	 getResponse().setContentLength(data.length);
				 getResponse().getOutputStream().write(data);
				 getResponse().getOutputStream().flush();
				 return null;
			}
		} catch (FileNotFoundException e) { 
			e.printStackTrace();
		} catch (IOException e) { 
			e.printStackTrace();
		}  
		return null;
	}
	
	//根据url
	public String downApk(){ 
		try {
			if(null==dirCode){
				return null;
			}
			Dir dir = Config.dirs.get(dirCode);
			if(null== dir){
				 return null;
			}
			if(!getBasePath2().equals(dir.getDownhost())){
				return null;
			}
			String uri = getRequest().getParameter("uri");
			File newFile = new File(dir.getRootpath()+"/"+"ycs_mobileapp/android_down/"+uri);
			if (newFile.exists() && newFile.isFile()){
				 FileInputStream fis = new FileInputStream(newFile);
				 int length = (int)newFile.length();
				 byte[] data = new byte[length];
				 fis.read(data);
				 fis.close();
				 getResponse().setContentType("application/octet-stream");
				 getResponse().setContentLength(data.length);
				 getResponse().getOutputStream().write(data);
				 getResponse().getOutputStream().flush();
				 return null;
			}
		} catch (FileNotFoundException e) { 
			e.printStackTrace();
		} catch (IOException e) { 
			e.printStackTrace();
		}  
		return null; 
	}
	
	/**
	 * 
	 * @return
	 */
	public String downSound(){
	    		//1.找到文件
	  		try { 
	  			String fileName =imageId+"-"+signCode+"_"+imageSuffix;
	  			if(null==dirCode){
	  				return null;
	  			}
	  			Dir dir = Config.dirs.get(dirCode);
	  			if(null== dir){
	  				 return null;
	  			}
	  			if(!getBasePath2().equals(dir.getAccesshost())){
	  				return null;
	  			}
	  			
	  			String fileAllName = dir.getRootpath()+"/ressd"+FileHandle.parsePath(imageId)+File.separator+fileName;
	  			File file = new File(fileAllName);  
	  			if (file.exists() && file.isFile()){
	  			    	 FileInputStream fis = new FileInputStream(file);
					 int length = (int)file.length();
					 byte[] data = new byte[length];
					 fis.read(data);
					 fis.close();
					 getResponse().setContentType("audio/mp3");
					 getResponse().setContentLength(data.length);
					 getResponse().getOutputStream().write(data);
					 getResponse().getOutputStream().flush();
					 return null;
	  			} 
	  		} catch (Exception e) { 
	  			e.printStackTrace();
	  		} 
	  		return null;
	}
	
	/**
	 * 
	 * @return
	 */
	public String downvideo(){
	    	//1.找到文件
		try { 
			String fileName =imageId+"-"+signCode+"_"+imageSuffix;
			if(null==dirCode){
				return null;
			}
			Dir dir = Config.dirs.get(dirCode);
			if(null== dir){
				 return null;
			}
			/*if(!getBasePath2().equals(dir.getAccesshost())){
				return null;
			}*/
			
			String fileAllName = dir.getRootpath()+"/resvd"+FileHandle.parsePath(imageId)+File.separator+fileName;
			File file = new File(fileAllName);  
			if (file.exists() && file.isFile()){
			    	 FileInputStream fis = new FileInputStream(file);
				 int length = (int)file.length();
				 byte[] data = new byte[length];
				 fis.read(data);
				 fis.close();
				 getResponse().setContentType("video/mp4");
				 getResponse().setContentLength(data.length);
				 getResponse().getOutputStream().write(data);
				 getResponse().getOutputStream().flush();
				 return null;
			} 
		} catch (Exception e) { 
			e.printStackTrace();
		} 
		return null;
	}
	

	public Integer getWidth() {
		return width;
	}

	public void setWidth(Integer width) {
		this.width = width;
	}

	public Integer getHeight() {
		return height;
	}

	public void setHeight(Integer height) {
		this.height = height;
	}

	public Long getImageId() {
		return imageId;
	}

	public void setImageId(Long imageId) {
		this.imageId = imageId;
	}

	public String getImageSuffix() {
		return imageSuffix;
	}

	public void setImageSuffix(String imageSuffix) {
		this.imageSuffix = imageSuffix;
	}

	public Long getImageLength() {
		return imageLength;
	}

	public void setImageLength(Long imageLength) {
		this.imageLength = imageLength;
	}

	public String getDirCode() {
		return dirCode;
	}

	public void setDirCode(String dirCode) {
		this.dirCode = dirCode;
	}

	public String getSignCode() {
	    return signCode;
	}

	public void setSignCode(String signCode) {
	    this.signCode = signCode;
	}
	
	
	
	
}
